package com.matchme;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CombineActivity  extends Activity implements OnClickListener {

	
	private static final int CAMERA_ACTIVITY1 = 0;
	private static final int CAMERA_ACTIVITY2 = 1;


	Button b1;
	Button b2;
	Button combine;
	ImageView im;
	
	Intent cameraIntent;
	Bitmap image1;
	Bitmap image2;
	String fileLoc;
	

	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.combine_image_view);
	        
	        b1 = (Button)findViewById(R.id.takePhoto1);
	        b2 = (Button) findViewById(R.id.takePhoto2);
	        combine = (Button) findViewById(R.id.combine);
	        
	        im = (ImageView) findViewById(R.id.combinedImage);
	        b1.setOnClickListener(this);
	        b2.setOnClickListener(this);
	        combine.setOnClickListener(this);

	    }


		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			fileLoc = Environment.getExternalStorageDirectory().getAbsolutePath() +  System.currentTimeMillis() + ".jpg";
			cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
			if(arg0.getId() == R.id.takePhoto1)
			{
			 
				Intent in = new Intent(this, CameraPreview.class);
				  this.startActivityForResult(in, CAMERA_ACTIVITY1);
			//cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
				//	Uri.fromFile(new File("/sdcard/tmp"))); 
			//startActivityForResult(cameraIntent, CAMERA_ACTIVITY1);
			
			
			}else if(arg0.getId() == R.id.takePhoto2)
			{
				 
				 
				cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
						Uri.fromFile(new File("/sdcard/tmp"))); 
				startActivityForResult(cameraIntent, CAMERA_ACTIVITY2);
			}else
			{
				Bitmap nImage1 = getResizedBitmap(image1, 640, 853);
				Bitmap nImage2 = getResizedBitmap(image2, 640, 853);
				Bitmap b = Bitmap.createBitmap(nImage1.getWidth()*2, nImage1.getHeight(), Bitmap.Config.RGB_565);
				Canvas c = new Canvas(b);
				Paint p = new Paint();
				c.drawBitmap(nImage1, 0, 0, p);
				c.drawBitmap(nImage2, nImage1.getWidth(), 0, p);
				
				String url = Images.Media.insertImage(getContentResolver(), b, "testIMage", null);
				im.setImageBitmap(b);
				
				
			}
		}
	
		
		@Override
		protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
			super.onActivityResult(requestCode, resultCode, intent);
			
			if (resultCode == RESULT_CANCELED) {
				//showToast(this,"Activity cancelled");
				return;
			}
			
			
			  FileInputStream in;
		        BufferedInputStream buf;
		        File fi = new File("/sdcard/tmp");
			switch (requestCode) {		
			case CAMERA_ACTIVITY1: 
				
		        try {
		       	    in = new FileInputStream("/sdcard/tmp");
		            buf = new BufferedInputStream(in);
		            image1 = BitmapFactory.decodeStream(buf);
		            //image.setImageBitmap(bMap);
		            if (in != null) {
		         	in.close();
		            }
		            if (buf != null) {
		         	buf.close();
		            }
		        } catch (Exception e) {
		            Log.d("Error reading file", e.toString());
		        }
				break;
			case CAMERA_ACTIVITY2: 
				try {
					in = new FileInputStream("/sdcard/tmp");
		            buf = new BufferedInputStream(in);
		            image2 = BitmapFactory.decodeStream(buf);
		            //image.setImageBitmap(bMap);
		            if (in != null) {
		         	in.close();
		            }
		            if (buf != null) {
		         	buf.close();
		            }
		        } catch (Exception e) {
		            Log.d("Error reading file", e.toString());
		        }
				break;
			}
		}
		
		public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

			int width = bm.getWidth();
	
			int height = bm.getHeight();

			float scaleWidth = ((float) newWidth) / width;

			float scaleHeight = ((float) newHeight) / height;

			// CREATE A MATRIX FOR THE MANIPULATION

			Matrix matrix = new Matrix();

			// RESIZE THE BIT MAP

			matrix.postScale(scaleWidth, scaleHeight);

			// RECREATE THE NEW BITMAP

			Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

			return resizedBitmap;
	
			}

}
